package com.rede.didiok.rank.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.utils.PageUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.rank.entity.AttrEntity;
import com.rede.didiok.rank.entity.AttrGroupEntity;
import com.rede.didiok.rank.service.AttrAttrgroupRelationService;
import com.rede.didiok.rank.service.AttrGroupService;
import com.rede.didiok.rank.service.AttrService;
import com.rede.didiok.rank.service.RankCategoryService;
import com.rede.didiok.rank.vo.AttrGroupRelationVo;
import com.rede.didiok.rank.vo.AttrGroupWithAttrsVo;


/**
 * 榜单成员的属性分组表
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-07-20 22:09:24
 */
@RestController
@RequestMapping("rank/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private RankCategoryService rankCategoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下所有的属性分组和属性
     * @param catUid
     * @return
     */
    @GetMapping("/{catId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catId") String catUid){
        // 查询当前分类下的所有属性分组
        // 查询每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catUid);
        return R.ok().put("data",vos);
    }

    /**
     * 新增分组与属性关联关系。
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        // // 添加属性分组与属性关联关系
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 查询分组未关联的属性，分组与属性的关系1 ：N。
     * @param groupId
     * @param params
     * @return
     */
    @GetMapping("/{groupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("groupId") Long groupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,groupId);

        return R.ok().put("page",page);
    }

    /**
     * 根据属性分组ID查询所有关联的属性
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") String catUid){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catUid);

        return R.ok().put("page", page);
    }


    /**
     * 回显信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("rank:attrgroup:info")
    public R info(@PathVariable("attrGroupId") String attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        String catelogUid = attrGroup.getCatUid();
        String[] path = rankCategoryService.findCatelogPath(catelogUid);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("rank:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
        attrGroupService.saveDetail(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("rank:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateDetail(attrGroup);

        return R.ok();
    }

    /**
     * 删除关联关系，批量删除。
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("rank:attrgroup:delete")
    public R delete(@RequestBody Long[] groupIds){
		attrGroupService.removeByIds(Arrays.asList(groupIds));

        return R.ok();
    }

}
